package com.edu.test;

import com.edu.bean.Depart;
import com.edu.bean.Emp;
import com.edu.bean.EmpVo;
import com.edu.mapper.EmpMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

public class MyTest02 {

    private SqlSession sqlSession = null;

    @Before
    public void init() throws IOException {
        String path = "mybatis-config.xml";

        InputStream inputStream = Resources.getResourceAsStream(path);

        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        sqlSession = sqlSessionFactory.openSession();
    }

    @Test
    public void test01(){
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
        List<Emp> emp = empMapper.queryEmp();
            System.out.println(emp);
    }

    @Test
    public void test02(){
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
        Map<String,Object> empMap = empMapper.queryEmpById(4);
        System.out.println(empMap);
    }

    @Test
    public void test03(){
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
        List<Emp> emp2 = empMapper.queryEmpByAge(20,30);
        for (Emp emp : emp2) {
            System.out.println(emp);
        }

    }

    @Test
    public void test04(){
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
        List<Emp> emp2 = empMapper.queryEmpBySex("女");
        System.out.println(emp2);
    }

    @Test
    public void test05(){
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
        int count = empMapper.queryCountBySex("男");
        System.out.println(count);
        }

    @Test
    public void test06(){
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
        List<Emp> emp = empMapper.queryEmpByName("殷");
        System.out.println(emp);
    }

    @Test
    public void test07(){
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
        Emp emp = empMapper.queryEmpAndDepartById(11);
        System.out.println(emp);
    }

    @Test
    public void test08(){
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
        List<Depart> departs = empMapper.queryDepartId();//获取部门id
        for (int i = 0; i <departs.size(); i++) {
            List<Map<String, Object>> mapList = empMapper.queryEmp01(departs.get(i).getDepartId());//传入部门id查询所有员工和部门信息
            for (Map<String, Object> stringObjectMap : mapList) {
                System.out.println(stringObjectMap);
            }
        }
    }


    @Test
    public void test09(){
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
        List<EmpVo> emp = empMapper.queryEmpAndDepartBySex("男");
//        for (EmpVo empVo : emp) {
//            System.out.println("员工id："+empVo.getEmpId()+"员工名字： "+empVo.getEmpName()+"员工年龄： "+empVo.getEmpAge()+"员工性别："+empVo.getEmpSex()
//            +"部门id："+empVo.getDepartId()+" 部门名字："+empVo.getDepartName()+"部门电话："+empVo.getPhoneNum());
//        }
        for (EmpVo empVo : emp) {
            System.out.println(empVo);
        }

    }

}
